//
//  EventViewController.m
//  QRCodeManager
//
//  Created by Cosmin Stirbu on 4/17/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "EventViewController.h"

@interface EventViewController ()

@end

@implementation EventViewController

@synthesize qrEvent;

#define DATE_FORMAT @"dd - MMM - YYYY\nHH:mm"

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewWillAppear:(BOOL)animated {
    // Title
    _titleLabel.text = qrEvent.title;
    
    // Dates
    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    [dateFormatter setDateFormat:DATE_FORMAT];
    _startDateLabel.text = [dateFormatter stringFromDate:qrEvent.startdate];
    _endDateLabel.text = [dateFormatter stringFromDate:qrEvent.enddate];
    
    // Location
    _locationLabel.text = qrEvent.location;
    
    // Description
    _descriptionTextView.text = qrEvent.summary;
    
    [super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section == 2) {
        // Add To Calendar
        [self handleCalendarOperation];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 1)
        return NSLocalizedString(@"table_description_header", nil);
    return nil;
}

#pragma mark - Calendar Operation

- (void)handleCalendarOperation {
    EKEventStore *es = [[EKEventStore alloc] init];
    if ([es respondsToSelector:@selector(requestAccessToEntityType:completion:)]) {
        // IOS 6
        [es requestAccessToEntityType:EKEntityTypeEvent completion:^(BOOL granted, NSError *error) {
            /* This code will run when uses has made his/her choice */
            if (granted) {
                // Add Event
                [self performSelectorOnMainThread:@selector(addEventInCalendarWithEKEventViewController) withObject:nil waitUntilDone:NO];
            }
        }];
    }  else {
        // Add Event
        [self addEventInCalendarWithEKEventViewController];
    }
}

- (void)addEventInCalendarWithEKEventViewController{
    EKEventEditViewController *eventViewController = [EKEventEditViewController new];
    EKEventStore *eventStore = [[EKEventStore alloc] init];
    EKEvent *event  = [EKEvent eventWithEventStore:eventStore];
    // Title
    event.title = qrEvent.title;
    
    // Start Date + End Date
    event.startDate = qrEvent.startdate;
    event.endDate = qrEvent.enddate;
    
    // Summary
    event.notes = qrEvent.summary;
    
    // Location
    event.location = qrEvent.location;
    
    [event setCalendar:[eventStore defaultCalendarForNewEvents]];
    
    eventViewController.event = event;
    eventViewController.eventStore = eventStore;
    
    eventViewController.editViewDelegate = self;
    [self.navigationController presentModalViewController:eventViewController animated:YES];
}

#pragma mark - Event Did Finish Editing

- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
    [controller dismissModalViewControllerAnimated:YES];
}

- (void)viewDidUnload {
    _titleLabel = nil;
    _startDateLabel = nil;
    _endDateLabel = nil;
    _locationLabel = nil;
    _descriptionTextView = nil;
    [super viewDidUnload];
}
@end
